package com.bawei.module_main.tab

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.bawei.module_main.R
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem

class NormalTabView @JvmOverloads constructor(context:Context,attrs:AttributeSet?=null,defaultStyle:Int=0):BaseTabItem(context,attrs) {

    private lateinit var mIcon:ImageView
    private lateinit var mTitle:TextView
    private lateinit var mMsg:RoundMessageView

    private lateinit var mDefaultIcon:Drawable
    private lateinit var mCheckedIcon:Drawable

    private lateinit var mTabTitle:String
    private var mChecked=false
    private var mMsgNum=0

    private var mDefaultTextColor = 0x56000000
    private var mCheckedTextColor = 0x56000000

    init {
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.tab_normal, this, true)
        //获取UI组件
        mIcon = findViewById(R.id.tab_icon)
        mTitle = findViewById(R.id.tab_title)
        mMsg = findViewById(R.id.tab_msg)
    }

    fun initData(@DrawableRes defaultIcon: Int, @DrawableRes checkedIcon: Int, title: String, tabChecked: Boolean = false, msgNum: Int = 0){
        mDefaultIcon = ContextCompat.getDrawable(context, defaultIcon)!!
        mCheckedIcon = ContextCompat.getDrawable(context, checkedIcon)!!

        mTabTitle = title
        mChecked = tabChecked
        mMsgNum = msgNum

        mTitle.text = mTabTitle

    }

    fun setDefaultTextColor(@ColorRes colorId: Int){
        mDefaultTextColor = ContextCompat.getColor(context, colorId)
    }

    fun setCheckedTextColor(@ColorRes colorId: Int){
        mCheckedTextColor = ContextCompat.getColor(context, colorId)
    }


    override fun setChecked(checked: Boolean) {
      if(checked){
          mIcon.setImageDrawable(mCheckedIcon)
          mTitle.setTextColor(mCheckedTextColor)
      }else{
          mIcon.setImageDrawable(mDefaultIcon)
          mTitle.setTextColor(mDefaultTextColor)
      }
        mChecked = checked
    }

    override fun setMessageNumber(number: Int) {
       mMsg.messageNumber=number
        mMsgNum=number
    }

    override fun setHasMessage(hasMessage: Boolean) {
       mMsg.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String?) {
        mTitle.text = title
        mTabTitle = title!!
    }

    override fun setDefaultDrawable(drawable: Drawable?) {
       if(drawable!=null){
           mDefaultIcon=drawable
       }
    }

    override fun setSelectedDrawable(drawable: Drawable?) {
        if (drawable != null){
            mCheckedIcon = drawable
        }

        if (mChecked){
            mIcon.setImageDrawable(drawable)
        }
    }

    override fun getTitle(): String {
        return mTitle.text.toString()
    }
}